﻿package ReadWriteXML;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.app.DTO.RoleDTO;
import com.app.PhanQuyen.Quyen;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

public class ReadXML {

	public static Document BuildXML(String path) {
		Document doc = null;
		DocumentBuilder builder = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.newDocument();

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		File f = new File(path);
		if (f.exists() == true) {
			doc = DOMUltil.parse(path);
			
		}

		return doc;
	}
	public static Document buildFromAsset(Context context, String path){
		try {
			AssetManager as = context.getAssets();
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder buider = factory.newDocumentBuilder();
			Document doc = buider.parse(as.open(path));
			return doc;
		} 
		catch (Exception e) {
			Log.i("Phu", "Không thể parse xml file:"+path);
		}
		return null;
	}
	public static String getTextValue(Element e, String tagName) {
		String textVal = null;
		Node parent = e.getParentNode();
		NodeList nl = e.getElementsByTagName(tagName);
		if (nl != null && nl.getLength() > 0) {
			Element el = (Element) nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}
		return textVal;
	}

	/**
	 * Calls getTextValue and returns a int value
	 */
	public static int getIntValue(Element e, String tagName) {
		// in production application you would catch the exception
		return Integer.parseInt(getTextValue(e, tagName));
	}

	public static String getTextValue(String path, String tagName) {
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if (doc != null) {
			Node n = doc.getElementsByTagName(tagName).item(0);
			text = n.getTextContent();

		}
		return text;
	}

	public static int getIntValue(String path, String tagName) {
		Log.d("PEERDROID", "tagName: " + tagName);
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if (doc != null) {
			Node n = doc.getElementsByTagName(tagName).item(0);
			text = n.getTextContent();
			Log.d("PEERDROID", "text: " + text);
		}
		return Integer.valueOf(text);
	}

	public static String getTextValue(String path, String node, String attribute) {
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if (doc != null) {
			Element e = (Element) doc.getElementsByTagName(node).item(0);
			text = e.getAttribute(attribute);
		}
		return text;
	}

	public static ArrayList<String> getTextValueElement(String path,
			String element, String attribute) {
		Document doc = null;
		doc = BuildXML(path);
		ArrayList<String> strList = new ArrayList<String>();

		if (doc != null) {
			NodeList nlist = doc.getElementsByTagName(element);
			int n = nlist.getLength();
			for (int i = 0; i < n; i++) {
				Element e = (Element) nlist.item(i);
				String text = e.getAttribute(attribute);
				strList.add(text);
			}
		}
		return strList;
	}
	/**
	 * hàm trả về danh sách thông tin học viên: CMember
	 * @param doc
	 * @return
	 */
	public static ArrayList<CMember> ReadMember(Document doc)
	{
		ArrayList<CMember> memberList = new ArrayList<CMember>();

		if(doc!=null)
		{
			NodeList nlearner = doc.getElementsByTagName("Learner");
			int n = nlearner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nlearner.item(i);
					//có 3 phần tử: [0]: userName; [1]: ip; [2]: fullname
					ArrayList<String> learner = new ArrayList<String>();
					learner.add(e.getAttribute("UserName"));
					learner.add(getTextValue(e, "IP"));
					learner.add(getTextValue(e, "FullName"));
					
					CMember mem = new  CMember();
					mem.set_fullName(getTextValue(e, "FullName"));
					mem.set_ip(getTextValue(e, "IP"));
					mem.set_userName(e.getAttribute("UserName"));
					memberList.add(mem);
				}
			}

		}

		return memberList;
	}

	public static ArrayList<CMember> ReadMember(String path)
	{
		ArrayList<CMember> memberList = new ArrayList<CMember>();
		Document doc = BuildXML(path);
		if(doc!=null)
		{
			NodeList nlearner = doc.getElementsByTagName("Learner");
			int n = nlearner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nlearner.item(i);
					//có 3 phần tử: [0]: userName; [1]: ip; [2]: fullname
					ArrayList<String> learner = new ArrayList<String>();
					learner.add(e.getAttribute("UserName"));
					learner.add(getTextValue(e, "IP"));
					learner.add(getTextValue(e, "FullName"));
					
					CMember mem = new  CMember();
					mem.set_fullName(getTextValue(e, "FullName"));
					mem.set_ip(getTextValue(e, "IP"));
					mem.set_userName(e.getAttribute("UserName"));
					memberList.add(mem);
				}
			}

		}
		
		return memberList;
	}
	//chat rieng========================
	/**
	 * Hàm trả về danh sách các ip từ 1 file cụ thể
	 * @param doc
	 * @param path
	 * @return
	 */
	public static ArrayList<ArrayList<String>> ReadIP(Document doc, String path)
	{

		doc = BuildXML(path);
		ArrayList<ArrayList<String>> ipList = new ArrayList<ArrayList<String>>();
		ArrayList<String> learnerList = new ArrayList<String>();
		if(doc!=null)
		{

			NodeList learner = doc.getElementsByTagName("Learner");
			int n = learner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)learner.item(i);
					learnerList.add(e.getAttribute("ID"));
					learnerList.add(getTextValue(e, "IP"));
					ipList.add(learnerList);
				}
			}

		}
		return ipList;
	}
	public static ArrayList<String> ReadIP(Document doc, String fName, String sUsername)
	{
		ArrayList<String> arr = new ArrayList<String>();
		if(doc != null)
		{
			NodeList resList = doc.getElementsByTagName("Learner");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
				if(e.getAttribute("UserName").equals(sUsername))
				{
				  	arr.add(getTextValue(e, "IP"));
				}
			}	
		}
		return arr;
	}
	public static ArrayList<String> ReadIP(String path)
	{
		Document doc = ReadXML.BuildXML(path);
		ArrayList<String> arr = new ArrayList<String>();
		if(doc != null)
		{
			NodeList resList = doc.getElementsByTagName("Learner");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
				arr.add(getTextValue(e, "IP"));
			}	
		}
		return arr;
	}
//end chat rieng===============================
	
}